package com.ruoyi.app.controller.user;

import com.ruoyi.app.business.account.CompanyAccountBusiness;
import com.ruoyi.app.business.user.UserBusiness;
import com.ruoyi.app.config.annotation.OperationAuth;
import com.ruoyi.app.domain.account.CompanyAccount;
import com.ruoyi.app.domain.my.PlatformMy;
import com.ruoyi.app.domain.user.add.UserInfoAdd;
import com.ruoyi.app.domain.user.add.UserInfoView;
import com.ruoyi.app.domain.user.inviteCode.InviteCode;
import com.ruoyi.app.domain.user.inviteCode.InviteCodeParam;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.common.core.web.domain.APIResult;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.service.account.query.CompanyAccountQuery;
import com.ruoyi.service.user.enums.UserTypeEnum;
import com.ruoyi.service.user.query.UserQuery;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 平台方用户
 *
 * @author Zane
 * @since 2025-09-27
 */
@OperationAuth(UserTypeEnum.PLATFORM_USER)
@RestController
@RequestMapping("/platform/")
public class PlatformController {

  @Resource
  private UserBusiness userBusiness;
  @Resource
  private CompanyAccountBusiness companyAccountBusiness;


  /**
   * 生成邀请码
   */
  @PostMapping("createInviteCode")
  public APIResult<InviteCode> createInviteCode(
      @Validated @RequestBody InviteCodeParam inviteCodeParam) {
    InviteCode inviteCode = userBusiness.createInviteCode(inviteCodeParam);
    return APIResult.success(inviteCode);
  }

  /**
   * 注销
   *
   * @param userId     用户id
   * @param deleteFlag 1:注销 0:不注销
   */
  @PostMapping("user/delete")
  public APIResult<Void> userDelete(@RequestParam("userId") Long userId,
      @RequestParam("deleteFlag") Integer deleteFlag) {
    userBusiness.userDelete(userId, deleteFlag);
    return APIResult.success();
  }

  /**
   * 封号(解封)
   *
   * @param userId  用户id
   * @param banFlag 1:封号 0:解封
   */
  @PostMapping("user/ban")
  public APIResult<Void> userBan(@RequestParam("userId") Long userId,
      @RequestParam("banFlag") Integer banFlag) {
    userBusiness.userBan(userId, banFlag);
    return APIResult.success();
  }

  /**
   * 认证用户
   */
  @PostMapping("user/auth")
  public APIResult<Void> userAuth(@RequestParam("userId") Long userId,
      @RequestParam("authFlag") Integer authFlag) {
    userBusiness.userAuth(userId, authFlag);
    return APIResult.success();
  }
  /**
   * 账户管理-账户列表
   */
  @PostMapping("userManageList")
  public TableDataInfo<UserInfoView> demanderUserManageList(
      @Validated @RequestBody UserQuery query) {
    final CommonPageInfo<UserInfoView> userView = userBusiness.demanderUserManageList(query);
    return CommonUtils.buildTableData(userView);
  }
  /**
   * 创建项目方
   */
  @PostMapping("addUser")
  public APIResult<Void> addUser(@RequestBody UserInfoAdd userInfo) {
    userBusiness.addUserByDemander(userInfo);
    return APIResult.success();
  }

  /**
   * 我的页面
   */
  @PostMapping("myInfo")
  public APIResult<PlatformMy> platformMyInfo() {
    PlatformMy platformMy = userBusiness.platformMyInfo();
    return APIResult.success(platformMy);
  }

  /**
   * 收支明细
   */
  @PostMapping("/company/accountList")
  public TableDataInfo<CompanyAccount> companyAccountList(
      @Validated @RequestBody CompanyAccountQuery query) {
    CommonPageInfo<CompanyAccount> accountList = companyAccountBusiness.companyAccountListForPlatform(query);
    return CommonUtils.buildTableData(accountList);
  }
}
